import InputManager from "./InputManager";
import TipPanel from "../UI/TipPanel";
import SpriteManager from "./SpriteManager";
import GamePlayPanel from "../UI/GamePlayPanel";
import WinLosePanel from "../UI/WinLosePanel";
import ResurrectionPanel from "../UI/ResurrectionPanel";
import Cell from "../Core/Cell";
import CellFunctions from "../Core/CellFunctions";
import LoadingPanel from "../UI/LoadingPanel";
import StartPanel from "../UI/StartPanel";

// 游戏状态
export enum GameState
{
    Free,
    Fall,
    Match,
    Bomb,
    Pause, //游戏暂停状态
    WinLose, //胜利失败状态
}

// 游戏玩法
export enum GameType
{
    Normal, //普通
    Speed, //竞速
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {
    public static Instance:GameManager;

    @property(cc.Node)
    Node_2D:cc.Node = null;

    id:number = 100;

    @property
    row:number = 10;
    @property
    col:number = 15;
    @property
    add_row:number = 2;

    @property(cc.Integer)
    type_count:number = 5;

    cellList : Array<Array<Cell>> = new Array<Array<Cell>>();
    movingCellList: Array<Cell> = new Array<Cell>();
    killCellList: Array<Array<Cell>> = new Array<Array<Cell>>();

    @property(cc.Prefab)
    Prefab_Cell:cc.Prefab = null;
    @property(cc.Prefab)
    Prefab_Grid:cc.Prefab = null;

    keyCode:number = -1;

    width:number;
    height_screen:number;

    @property(cc.Float)
    padding_horizontal:number = 100;

    @property(cc.Float)
    padding_bottom:number = 140;
    cellSize:number;

    gameState:GameState;
    gameType:GameType;
    score:number = 0;

    @property(cc.Integer)
    timerCount:number = 1;
    @property(cc.Float)
    timer:number = 0;

    isKill:boolean = false;

    userInfo:any;

    onLoad () {
        GameManager.Instance = this;
    }

    start () {
        console.log(123);

        
        // if (cc.sys.platform == cc.sys.WECHAT_GAME) {
        //     let button = wx.createUserInfoButton({
        //         type: 'text',
        //         text: 'tap to start',
        //         style: {
        //         left: 0,
        //         top: 0,
        //         width: wx.getSystemInfoSync().screenWidth,
        //         height: wx.getSystemInfoSync().screenHeight,
        //         lineHeight: 40,
        //         backgroundColor: '#ffffff',
        //         color: '#000000',
        //         textAlign: 'center',
        //         fontSize: 16,
        //         borderRadius: 4
        //         }
        //     })
        //     button.onTap((res) => {
        //         console.log(res)
        //         GameManager.Instance.userInfo = res;
        //         console.log(GameManager.Instance.userInfo);
        //         button.hide();
    
        //         // wx.showToast({
        //         //     title: '成功',
        //         //     icon: 'success',
        //         //     duration: 2000
        //         // })
    
        //         LoadingPanel.Instance.ShowOff();
        //         StartPanel.Instance.ShowOn();
        //     })
        // }
        

        this.width = cc.winSize.width - this.padding_horizontal; //26
        this.height_screen = cc.winSize.height;
        this.cellSize = this.width / this.col;


        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            let button = wx.createUserInfoButton({
                type: 'text',
                text: 'tap to start',
                style: {
                left: 0,
                top: 0,
                width: wx.getSystemInfoSync().screenWidth,
                height: wx.getSystemInfoSync().screenHeight,
                lineHeight: 40,
                backgroundColor: '#ffffff',
                color: '#000000',
                textAlign: 'center',
                fontSize: 16,
                borderRadius: 4
                }
            })
            button.onTap((res) => {
                console.log(res)
                GameManager.Instance.userInfo = res;
                console.log(GameManager.Instance.userInfo);
                button.hide();
    
                // wx.showToast({
                //     title: '成功',
                //     icon: 'success',
                //     duration: 2000
                // })
    
                LoadingPanel.Instance.ShowOff();
                StartPanel.Instance.ShowOn();
            })  
        }
    }

    update(dt)
    {
        if(GameManager.Instance.gameType == GameType.Speed && GameManager.Instance.gameState == GameState.Free)
        {
            this.timer += dt;
            if (this.timer >= this.timerCount) {
                this.timer = 0;
                
                let row = CellFunctions.GetCurrentMovingRow();
                let currentMovingY = row - 1;
                
                if (currentMovingY - CellFunctions.GetMaxY() >= 2)
                {
                    this.timer = 0;
                    
                    for (let x = 0; x <= GameManager.Instance.col - 1; x++)
                    {
                        GameManager.Instance.cellList[x][currentMovingY - 1] = GameManager.Instance.cellList[x][currentMovingY];
                        let cell:Cell = GameManager.Instance.cellList[x][currentMovingY - 1];
                        cell.y -= 1;
                        cell.Refresh();

                        GameManager.Instance.cellList[x][currentMovingY] = null;
                    }

                    let row2 = CellFunctions.GetCurrentMovingRow();
                    let currentMovingY2 = row2 - 1;
                    if (currentMovingY2 - CellFunctions.GetMaxY() < 2)
                    {
                        GameManager.Instance.scheduleOnce(function(){
                            CellFunctions.MoveDownAllCell();
                        }, 1);
                    }
                }

            }
        }

        if (GameManager.Instance.gameState == GameState.Free)
        {

        }
        else if (GameManager.Instance.gameState == GameState.Fall)
        {
            
        }
        else if (GameManager.Instance.gameState == GameState.Match)
        {

        }
        else if (GameManager.Instance.gameState == GameState.Bomb)
        {

        }
    }

    public ChangeStateMachine(_state:GameState)
    {
        // cc.log(GameState[_state]);
        GameManager.Instance.gameState = _state;

        if (GameManager.Instance.gameState == GameState.Free)
        {

        }
        else if (GameManager.Instance.gameState == GameState.Fall)
        {
            CellFunctions.MoveDownAllCell();
        }
        else if (GameManager.Instance.gameState == GameState.Match)
        {
            CellFunctions.KillAllSameList();
        }
        else if (GameManager.Instance.gameState == GameState.Bomb)
        {
            CellFunctions.Bomb();
        }
    }

    public Clear()
    {
        CellFunctions.ClearAll();
    }

    public GameStart(_score:number = 0)
    {
        this.Clear();

        this.timer = 0;
        GameManager.Instance.isKill = false;
        GameManager.Instance.gameState = GameState.Free;
        GameManager.Instance.score = _score;
        // GamePlayPanel.Instance.RefreshScore(GameManager.Instance.score);

        for (var x = 0; x <= this.col - 1; x++)
        {
            this.cellList[x] = new Array<Cell>();
            for (var y = 0; y <= this.row - 1 + this.add_row; y++)
            {
                var grid:cc.Node = CellFunctions.CreateGrid(x, y, this.cellSize);
                var sprite = grid.getComponentInChildren(cc.Sprite);
                // if (x % 2 == 0)
                // {
                //     sprite.spriteFrame = SpriteManager.Instance.spriteFrameLists.bgFrameList[0];
                // }
                // else
                // {
                //     sprite.spriteFrame = SpriteManager.Instance.spriteFrameLists.bgFrameList[1];
                // }
                if (y > this.row - 1)
                {
                    sprite.node.opacity = 150;
                }
            }
        }

        CellFunctions.CreateTopLine(this.row + 1);
        CellFunctions.CreateTopLine(this.row + 2);

        GamePlayPanel.Instance.ShowOn();
    }

    // 游戏结束
    public GameOver()
    {
        this.Clear();
        GamePlayPanel.Instance.ShowOff();
    }

    // 复活
    public Resurrection()
    {
        GameManager.Instance.GameStart(GameManager.Instance.score);
    }
    
    //一回合消除部分结束
    public RoundEnd()
    {
        GameManager.Instance.scheduleOnce(function(){
            this.StartNewRound();
        }, 0.05);
    }

    //开始新的回合
    public StartNewRound()
    {
        CellFunctions.SetAllNotNew();

        if(CellFunctions.GetMaxY() >= GameManager.Instance.row - 1)
        {
            // TipPanel.Instance.Show("游戏失败");
            GameManager.Instance.gameState = GameState.WinLose;
            ResurrectionPanel.Instance.ShowOn();
        }
        else
        {
            for (let x = 0; x <= GameManager.Instance.col - 1; x++)
            {
                GameManager.Instance.cellList[x][this.row + 1 - 1] = GameManager.Instance.cellList[x][this.row + 2 - 1];
                let cell:Cell = GameManager.Instance.cellList[x][this.row + 1 - 1];
                cell.y -= 1;
                cell.RefreshPos();
            }

            CellFunctions.CreateTopLine(this.row + 2);

            GameManager.Instance.gameState = GameState.Free;
            GameManager.Instance.isKill = false;
        }
    }

}